package ru.tatintec.gwttest.server.impl;

import java.util.List;
import ru.tatintec.gwttest.client.service.EmployeeCatalogService;
import ru.tatintec.gwttest.server.dao.EmployeeCatalog;
import ru.tatintec.gwttest.shared.Employee;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class EmployeeCatalogServiceImpl extends RemoteServiceServlet implements EmployeeCatalogService {

	/**
	 */
	private List<Employee> emplList = EmployeeCatalog.INSTANCE();


	@Override
	public List<Employee> getEmployeeList() {
		return emplList;
	}
	
	@Override
	public void createEmployee(Employee employee) {
		employee.setId(""+System.currentTimeMillis());
		emplList.add(employee);
	}

	@Override
	public void updateEmployee(Employee employee) {
		String employeeId = employee.getId();
		int index = -1;
		for (Employee empl : emplList) {
			if (empl.getId().equals(employeeId)) {
				index = emplList.indexOf(empl);
				break;
			}
		}
		if (index != -1) {
			emplList.remove(index);
			emplList.add(index, employee);
		}
	}

	@Override
	public void deleteEmployee(String employeeId) {
		int index = -1;
		for (Employee employee : emplList) {
			if (employee.getId().equals(employeeId)) {
				index = emplList.indexOf(employee);
				break;
			}
		}
		if (index != -1) {
			emplList.remove(index);
		}

	}


	@Override
	public void generateTestData() {
		EmployeeCatalog.generateTestData();
	}
}
